<?php

namespace app\crontab;

use zhanshop\App;
use zhanshop\Crontab;
use zhanshop\client\Httpclient;

class NginxStatusCrontab extends Crontab
{
    public function configure()
    {
        $this->setTitle("每分钟执行一次")->interval(60);
    }

    public function execute()
    {
        $outData = (new Httpclient())->request("http://zhanshop-nginx/zhanshop-status")['body'];
        if($outData){
            $connectionsLine = explode("\n", $outData);
            $arr = explode("Active connections: ", $connectionsLine[0]);
            $connections = 0;
            $accept = 0;
            $requests = 0;
            if(isset($arr[1])){
                $connections = intval($arr[1]);
            }
            if(isset($connectionsLine[2])){
                $arr = explode(" ", $connectionsLine[2]);
                $accept = intval($arr[2] ?? 0);
                $requests = intval($arr[3] ?? 0);
            }
            error_log(date('H:i').','.$connections.','.$accept.','.$requests."\n", 3, App::rootPath().'/docker/nginx/stats/connections-'.date('Ymd').'.log');
        }
    }
}